//
//  FirstInfoControl.m
//  FreeTime
//
//  Created by  on 16/8/27.
//  Copyright © 2016年 SRDZ.com. All rights reserved.
//

#import "FirstInfoControl.h"

#import "Header.h"

#import "DatePackerUtil.h"

#import "SexControl.h"

#import "UIView+TYAlertView.h"

#import "SettingModelView.h"

@interface FirstInfoControl (){
    UIDatePicker * datePicker ;
    UIView * dateBGView, *bgView;
//    UIView * bgView;
    BOOL isRepate;
    NSMutableArray * contentArr;
    int k;
}
@property (nonatomic,strong) UIDatePicker *pickerView;
@end
@implementation FirstInfoControl

- (void)viewDidLoad {
    [self setupNav];
    [self setupUI];
    [self setupDate];

}
#pragma mark -- selfStyle --
- (void)setupUI{
    
    
    isRepate = NO;
    k=0;
    contentArr = [NSMutableArray array];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dateEnter)name:UIKeyboardWillShowNotification object:nil];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"个人信息";
    NSArray * arr = @[@"昵称",@"生日",@"性别"];
    NSArray * placeArr = @[@"请输入你的昵称",@"请选择你的出生日期",@"请选择你的性别"];
    
    for (int i =0; i<arr.count; i++) {
        
        UILabel * chooseLbl = [ReduceCode creatLabelWithFrame:CGRectMake(10, 40*i+46.5*i, 70, 40) title:arr[i] titleColor:[ImageUtil colorWithRgb:0x4c4c4c] font:14 textAlignment:0];
        [ToolUtil createLine:39 superView:chooseLbl lift:0 width:55 hight:1 color:[ImageUtil colorWithRgb:0xb3b3b3]];
        [self.view addSubview:chooseLbl];
        if (i == 0) {
            UITextField * tf = [[UITextField alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(chooseLbl.frame), WindowWidth-20, 46.5)];
            tf.borderStyle = UITextBorderStyleNone;
            tf.font = FONT(14);
            tf.delegate = self;
            [tf addTarget:self action:@selector(dateEnter) forControlEvents:UIControlEventEditingChanged];
            tf.placeholder = placeArr[i];
            [ToolUtil createLine:35.5 superView:tf lift:0 width:WindowWidth-20 hight:1 color:[ImageUtil colorWithRgb:0xe6e6e6]];
            [self.view addSubview:tf];

        }else{
            UILabel * contentLbl = [ReduceCode creatLabelWithFrame:CGRectMake(10, CGRectGetMaxY(chooseLbl.frame), WindowWidth-20, 46.5) title:placeArr[i] titleColor:[ImageUtil colorWithRgb:0xcccccc] font:12 textAlignment:0];
            contentLbl.tag = 100+i;
            [contentArr addObject:contentLbl];
            contentLbl.userInteractionEnabled = YES;
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
            [contentLbl addGestureRecognizer:tap];
            [ToolUtil createLine:35.5 superView:contentLbl lift:0 width:WindowWidth-20 hight:1 color:[ImageUtil colorWithRgb:0xe6e6e6]];
            [self.view addSubview:contentLbl];
        }
    
    }
    
}
- (void)setupDate{
    
    dateBGView = [[UIView alloc] initWithFrame:CGRectMake(0, WindowHeight, WindowWidth, 200)];
    dateBGView.backgroundColor = [UIColor whiteColor];
    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 150)];
    int  year = [[[NSString stringWithFormat:@"%@",[NSDate date]] substringToIndex:4] intValue];
    NSString * str = [NSString stringWithFormat:@"%d",year-16];

    datePicker.maximumDate = [DateUtil creatDateWithYear:str month:@"12" day:@"31"];
    datePicker.minimumDate = [DateUtil creatDateWithYear:@"1970" month:@"1" day:@"1"];
    NSLocale * locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    datePicker.locale = locale;
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.backgroundColor = [UIColor whiteColor];
    
    UIButton * btn = [ReduceCode creatBtnWithFrame:CGRectMake(0, 150, WindowWidth, 50) btnTitle:@"确定" btnTitleColor:[UIColor whiteColor] btnTitleFont:14 btnBackgroundColor:HOME_COLOR backgroundImage:nil isCorner:NO];
    [btn addTarget:self action:@selector(dateEnter) forControlEvents:UIControlEventTouchUpInside];
    
    [dateBGView addSubview:datePicker];
    [dateBGView addSubview:btn];
    
    [self.view addSubview:dateBGView];

}
- (void)setupAlert{
    
    TYAlertView *alertView = [TYAlertView alertViewWithTitle:@"提示" message:@"性别选择后将不可再修改"];
    alertView.layer.cornerRadius = 10;
    [ToolUtil createLine:75 superView:alertView lift:15 width:250 hight:1 color:[ImageUtil colorWithRgb:0xe6e6e6]];
    [ToolUtil createVerticalLine:80 superView:alertView lift:140 height:30 color:[ImageUtil colorWithRgb:0xe6e6e6]];
    alertView.buttonCancleBgColor = [UIColor whiteColor];
    alertView.buttonDestructiveBgColor = [UIColor whiteColor];
    [alertView addAction:[TYAlertAction actionWithTitle:@"确定" style:TYAlertActionStyleDestructive handler:^(TYAlertAction *action) {
        NSLog(@"============%@",action.title);
        [self.navigationController pushViewController:[SexControl new] animated:YES];
        
    }]];
    [alertView addAction:[TYAlertAction actionWithTitle:@"取消" style:TYAlertActionStyleCancle handler:^(TYAlertAction *action) {
    }]];
    
    TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView preferredStyle:TYAlertControllerStyleAlert];
    [self presentViewController:alertController animated:YES completion:nil];
    
}
- (void)setupAlertControl{
    UILabel * lbl = contentArr[1];

    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"性别选择后不可更改" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction * cancleAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }];
    UIAlertAction * maleAction = [UIAlertAction actionWithTitle:@"女" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        lbl.text = @"女";
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }];
    UIAlertAction * feMaleAction = [UIAlertAction actionWithTitle:@"男" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        lbl.text = @"男";
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [alertController addAction:feMaleAction];
    [alertController addAction:maleAction];
    [alertController addAction:cancleAction];
    [self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark -- 响应事件 --
- (void)enterBtn{

}
- (void)clickBtn:(UIButton *)btn{
    [bgView removeFromSuperview];
    if (btn.tag == 200) {
        [self.navigationController pushViewController:[SexControl new] animated:YES];
    }else{
        
    }
}
- (void)tap:(UIGestureRecognizer*)tap{
    [self.view endEditing:YES];
    if (tap.view.tag == 101) {
        if (dateBGView.frame.origin.y<WindowHeight) {
            return;
        }else{

            [UIView animateWithDuration:0.4 animations:^{
                dateBGView.frame = CGRectMake(0, WindowHeight-264, WindowWidth, 200);
                isRepate = YES;

            }];
        }
    }else if (tap.view.tag == 102){
        [self setupAlertControl];
        [self dateEnter];
    }
    
}
- (void)dateEnter{
    UILabel * lbl = contentArr[0];
    NSString * str = [[NSString stringWithFormat:@"%@",datePicker.date] substringToIndex:10];
    lbl.text = str;
    [UIView animateWithDuration:0.4 animations:^{
        dateBGView.frame = CGRectMake(0, WindowHeight, WindowWidth, 200);
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.view endEditing:YES];

}
#pragma mark -- tfDelegate --
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [self enterBtn];
}
@end
